<?php
/**
 * 时间处理 辅助方法
 *
 * Author: Zeon
 * Date: 2017/11/14
 * Time: 17:19
 * Created by PhpStorm.
 */
namespace App\Services\Utils;


use Carbon\Carbon;

class TimeHelper
{
    /**
     * 尝试转成 Carbon 时间
     *
     * @example
     *      tryToCarbon('2010-10-10') => Carbon
     *      tryToCarbon('2010/10-10') => null
     *
     * @param $time_str
     * @return bool|Carbon
     */
    public static function tryToCarbon($time_str)
    {
        try {
            return new Carbon($time_str);
        } catch (\Exception $e) {
            return null;
        }
    }

    /**
     * 尝试转成 Carbon 时间，失败则返回当前时间
     *
     * @example
     *      tryToCarbon('2010-10-10') => Carbon
     *      tryToCarbon('2010/10-10') => Carbon::now()
     *
     * @param $time_str
     * @return bool|Carbon
     */
    public static function tryToCarbonOrNow($time_str)
    {
        return self::tryToCarbon($time_str) ?: Carbon::now();
    }

    /**
     * 尝试转成 Carbon Unix 开始时间
     *
     * @example
     *      tryToCarbon('2010-10-10') => new Carbon('2010-10-10')
     *      tryToCarbon('2010/10-10') => new Carbon('1970/1/1')
     *      tryToCarbon(null) => new Carbon('1970/1/1')
     *
     * @param string|null $time_str
     * @return bool|Carbon
     */
    public static function tryToCarbonOrUnixStart($time_str)
    {
        $unix_start =  new Carbon('1970/1/1');

        if (! $time_str) return $unix_start;

        return self::tryToCarbon($time_str) ?: $unix_start;
    }

    /**
     * 尝试转成 Carbon Unix （数据库timestamp字段能接受的合法值） 结束时间
     *
     * @example
     *      tryToCarbon('2010-10-10') => new Carbon('2010-10-10')
     *      tryToCarbon('2010/10-10') => new Carbon('2038/12/1')
     *      tryToCarbon(null) => new Carbon('2038/12/1')
     *
     * @param $time_str
     * @return bool|Carbon
     */
    public static function tryToCarbonOrUnixEnd($time_str)
    {
        $unix_end = new Carbon('2038/12/1');

        if (! $time_str) return $unix_end;

        return self::tryToCarbon($time_str) ?: $unix_end;
    }

    /**
     *  从数据库 获取 格式：1970-12-15
     *
     * @param string $carbon
     * @return string
     */
    public static function tryCarbonToString($carbon)
    {
        if ($carbon instanceof Carbon) {
            $date = $carbon->toDateString();
        } else {
            $date = null;
        }

        return $date;
    }

    /**
     * 如果日期是今天，同时时间的 00:00:00 ，刚返回当前时间
     *
     * @param Carbon $date
     * @return Carbon
     *
     * @test
     * @see TimeHelperTest::test_time_helper_toNowIfToday()
     */
    public static function toNowIfToday(Carbon $date)
    {
        $now = Carbon::now();

        if ($now->isSameDay($date) && ($date->hour == 0)) {
            return $now;
        } else {
            return $date;
        }
    }
    
    /**
     * 返回文件名后缀格式时间戳
     *
     * @param Carbon|null $time
     * @param string $format
     * @return string
     */
    public static function toFileSuffix(Carbon $time = null, string $format = 'Y-m-d_H_i_s'): string
    {
        $time = $time ?: Carbon::now();
        
        return $time->format($format);
    }

    /**
     * @param $seconds
     *
     * @return string
     */
    static public function secondToHourFormat($seconds)
    {
        $hours = floor($seconds / 3600);
        $minutes = floor(($seconds / 60) % 60);
        $seconds = $seconds % 60;
        return "$hours:$minutes:$seconds";
    }

    /**
     * 返回微秒级的时间流水号
     *
     * @link http://php.net/manual/en/function.date.php - format 参数参考
     *
     * @example
     *      "20180717164051383281"
     */
    public static function microTimeSerial(): string
    {
        return now()->format('YmdHisu');
    }
}